package net.example.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.oraycn.esbasic.helpers.StringHelper;

import net.example.activity.adapter.SelectableItemAdapter;
import net.example.manager.ClientGlobalCache;
import net.example.manager.Manager;
import net.example.model.BasicResult;
import net.example.model.SelectableItemModel;
import net.example.model.UpdatePropertyType;
import net.example.ggtalk.R;
import net.example.utils.IntentUtils;
import net.example.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class FriendCatalogActivity extends Activity {

    private String friendID = "";//friendID不为空表示对方已经是好友； 为空时表示新增好友
    private List<SelectableItemModel> modelList = new ArrayList<SelectableItemModel>();
    private SelectableItemAdapter adapter;
    private String currentSelectedCatalogName;
    private Intent intent=new Intent();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend_catalog);
        friendID = getIntent().getStringExtra("friendID");
        currentSelectedCatalogName=getIntent().getStringExtra(getResources().getString(R.string.Catalog));
        TextView title = (TextView) findViewById(R.id.headerText);
        title.setText("选择分组");
        ImageButton btn_more_detail = (ImageButton) findViewById(R.id.btn_more_detail);
        btn_more_detail.setImageResource(R.drawable.plus_btn);
        btn_more_detail.setVisibility(View.VISIBLE);
        this.initListView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        this.refreshModelList();
    }

    private void initListView() {
        ListView lv_Catalog = (ListView) findViewById(R.id.catalog_listView);
        adapter = new SelectableItemAdapter(this, this.modelList);
        lv_Catalog.setAdapter(adapter);
        lv_Catalog.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SelectableItemModel model=  modelList.get(position);
                if(model.Checked)
                {
                    return;
                }

                //friendID不为空表示对方已经是好友； 为空时表示新增好友
                if(StringHelper.isNullOrEmpty(friendID))
                {
                    intent.putExtra(getResources().getString(R.string.Catalog),model.Key);
                    setResult(RESULT_OK,intent);
                    finish();
                    return;
                }
                BasicResult result= Manager.getInstance().moveFriendCatalog(friendID,currentSelectedCatalogName,model.Key);

                if(result==BasicResult.Succeed)
                {
                    currentSelectedCatalogName=model.Key;
                    refreshModelList();
                }else {
                    ToastUtils.showShort(FriendCatalogActivity.this, "更改失败！");
                }
            }
        });
        lv_Catalog.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d("d","长点击"+position);
                return true;
            }
        });
        this.refreshModelList();
    }


    private void refreshModelList() {
        modelList.clear();

        List<String> catalogNameList = ClientGlobalCache.getInstance().getCurrentUser().getCatalogNameList();
        for (String catalogName : catalogNameList) {
            modelList.add(new SelectableItemModel(catalogName, catalogName.equals(currentSelectedCatalogName)));
        }
        modelList.add(new SelectableItemModel("黑名单","黑名单".equals(currentSelectedCatalogName)));
        adapter.notifyDataSetChanged();
    }

    //跳转到新增页面
    public void moveToMore(View view)
    {
        IntentUtils.startActivityForString(this, UpdatePropertyActivity.class, getResources().getString(R.string.update_property_type), UpdatePropertyType.AddCatalog.toString());
    }

    public void finish(View view)
    {
        finish();
    }


}
